<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    if (session.getAttribute("user") == null) {
        response.sendRedirect(request.getContextPath() + "/login.jsp");
        return;
    }

    com.company.ems.model.Employee employee =
            (com.company.ems.model.Employee) request.getAttribute("employee");

    if (employee == null) {
        response.sendRedirect("employees?action=list");
        return;
    }
%>
<html>
<head>
    <title>员工信息管理系统 - 编辑员工</title>
    <link rel="stylesheet" href="../../css/style.css">
</head>
<body>
<header>
    <div class="logo">员工信息管理系统</div>
    <nav>
        <ul>
            <li><a href="../../dashboard">首页</a></li>
            <li><a href="employees?action=list" class="active">员工管理</a></li>
        </ul>
    </nav>
    <div class="user-info">
        <span>欢迎, <%= ((com.company.ems.model.User)session.getAttribute("user")).getUsername() %></span>
        <a href="../../logout" class="logout-btn">退出登录</a>
    </div>
</header>

<main>
    <div class="container">
        <div class="header-actions">
            <h2>编辑员工</h2>
            <a href="employees?action=list" class="btn secondary">返回列表</a>
        </div>

        <% if (request.getAttribute("error") != null) { %>
        <div class="error-message"><%= request.getAttribute("error") %></div>
        <% } %>

        <form action="employees?action=edit" method="post" class="data-form">
            <input type="hidden" name="id" value="<%= employee.getId() %>">

            <div class="form-row">
                <div class="form-group">
                    <label for="name">姓名 <span class="required">*</span></label>
                    <input type="text" id="name" name="name" value="<%= employee.getName() %>" required>
                </div>
                <div class="form-group">
                    <label for="department">部门 <span class="required">*</span></label>
                    <input type="text" id="department" name="department" value="<%= employee.getDepartment() %>" required>
                </div>
            </div>

            <div class="form-row">
                <div class="form-group">
                    <label for="position">职位 <span class="required">*</span></label>
                    <input type="text" id="position" name="position" value="<%= employee.getPosition() %>" required>
                </div>
                <div class="form-group">
                    <label for="salary">薪资 <span class="required">*</span></label>
                    <input type="number" step="0.01" min="0" id="salary" name="salary"
                           value="<%= employee.getSalary() %>" required>
                </div>
            </div>

            <div class="form-row">
                <div class="form-group">
                    <label for="email">邮箱</label>
                    <input type="email" id="email" name="email" value="<%= employee.getEmail() %>">
                </div>
                <div class="form-group">
                    <label for="phone">电话</label>
                    <input type="text" id="phone" name="phone" value="<%= employee.getPhone() %>">
                </div>
            </div>

            <div class="form-row">
                <div class="form-group">
                    <label for="hireDate">入职日期 <span class="required">*</span></label>
                    <input type="date" id="hireDate" name="hireDate"
                           value="<%= new java.text.SimpleDateFormat("yyyy-MM-dd").format(employee.getHireDate()) %>"
                           required>
                </div>
            </div>

            <div class="form-actions">
                <button type="submit" class="btn success">更新</button>
                <a href="employees?action=list" class="btn secondary">取消</a>
            </div>
        </form>
    </div>
</main>

<footer>
    <p>&copy; 2023 员工信息管理系统 版权所有</p>
</footer>
</body>
</html>